﻿using App_Clinique.ClassLibrary.DbContext;
using System;
using System.Collections.Generic;
using System.Data;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace App_Clinique.ClassLibrary.Entities
{
    public partial class Consultation
    {
        /****************************************************************************************************
         * Methods - Inser, Update, Delete
         ***************************************************************************************************/
        public static void Create(Consultation consultation)
        {
            using (App_cliniqueContext db = new App_cliniqueContext())
            {
                db.Consultation.Add(consultation);
                db.SaveChanges();
            }
        }

        public static void Update(Consultation consultation)
        {
            using (App_cliniqueContext db = new App_cliniqueContext())
            {
                db.Entry(consultation).State = EntityState.Modified;
                db.SaveChanges();
            }
        }

        public static void Delete(Consultation consultation)
        {
            using (App_cliniqueContext db = new App_cliniqueContext())
            {
                db.Entry(consultation).State = EntityState.Deleted;
                db.SaveChanges();
            }
        }

        /****************************************************************************************************
         * Methods - Select
         ***************************************************************************************************/
        public static Consultation Get(Guid consultationID)
        {
            Consultation Obj = null;

            using (App_cliniqueContext db = new App_cliniqueContext())
            {
                Obj = (from c in db.Consultation
                       where c.ConsultationID == consultationID
                       select c).SingleOrDefault();
            }

            return Obj;
        }

        public static List<Consultation> GetList()
        {
            List<Consultation> objs = null;

            using (App_cliniqueContext db = new App_cliniqueContext())
            {
                objs = (from c in db.Consultation
                        select c).ToList();
            }

            return objs;
        }

        public static List<Consultation> GetList(Guid PatientID)
        {
            List<Consultation> objs = null;

            using (App_cliniqueContext db = new App_cliniqueContext())
            {
                objs = (from c in db.Consultation
                        where c.PatientID==PatientID
                        select c).ToList();
            }

            return objs;
        }
        public static List<Consultation> GetListOfWeek()
        {
            List<Consultation> objs = null;

            using (App_cliniqueContext db = new App_cliniqueContext())
            {
                DateTime today = DateTime.Today;
                int NumDay = (today.DayOfWeek == DayOfWeek.Sunday) ? 7 : (int)today.DayOfWeek;
                DateTime startOfWeek = today.AddDays(NumDay * -1);
                DateTime endOfWeek = startOfWeek.AddDays(8);

                objs = (from c in db.Consultation
                        where (c.DateConsultationPrevue >= startOfWeek && c.DateConsultationPrevue <= endOfWeek)
                        select c).ToList();

                objs.ForEach(ConsultationDB =>
                {
                    Patient PatientDB = (from p in db.Patient
                                         where p.PatientID == ConsultationDB.PatientID
                                         select p).FirstOrDefault();

                    ConsultationDB.Patient = PatientDB;
                });
            }

            return objs;
        }

        public static List<Consultation> GetListOfThisDay(DateTime ThisDay)
        {
            List<Consultation> objs = null;

            using (App_cliniqueContext db = new App_cliniqueContext())
            {
                objs = (from c in db.Consultation
                        where (
                            c.DateConsultationPrevue.Day == ThisDay.Day &&
                            c.DateConsultationPrevue.Month == ThisDay.Month &&
                            c.DateConsultationPrevue.Year == ThisDay.Year
                        )
                        select c).ToList();
            }

            return objs;
        }
    }
}
